package com.iyao_app.adapter;

import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.SceneUser;
import com.iyao_app.util.ResourcesUtils;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/21.
 */

public class LatelyPersonsBotoomAdapter extends BaseQuickAdapter<SceneUser, BaseViewHolder> {


    public LatelyPersonsBotoomAdapter(@Nullable List<SceneUser> data) {
        super(R.layout.item_picture_, data);

    }

    public void delete() {
        mData = null;
        mData = new ArrayList<>();
    }

    @Override
    protected void convert(final BaseViewHolder helper, SceneUser item) {
        final int position = helper.getLayoutPosition();
        final SimpleDraweeView sdvPhoto = helper.getView(R.id.sdv_photo);

        int width = ScreenUtils.getScreenWidth() / 5;

        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);

        sdvPhoto.setLayoutParams(params);

        final RoundingParams roundingParams = sdvPhoto.getHierarchy().getRoundingParams();

//        if (item.isSelect()) {
//            roundingParams.setBorderColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
//            sdvPhoto.getHierarchy()
//                    .setRoundingParams(roundingParams);
//        } else {
//            roundingParams.setBorderColor(Color.TRANSPARENT);
//            sdvPhoto.getHierarchy()
//                    .setRoundingParams(roundingParams);
//        }
//
//        sdvPhoto.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                for (int i = 0; i < mData.size(); i++) {
//                    if (position == i) {
//                        if (selectListener != null) {
//                            selectListener.onSelect(position);
//                        }
//                        mData.get(i).setSelect(true);
//                    } else {
//                        mData.get(i).setSelect(false);
//                    }
//                }
//                notifyDataSetChanged();
//            }
//        });
        if (TextUtils.isEmpty(item.avatarUrl)) {
            sdvPhoto.setAlpha(0.6f);
            roundingParams.setBorderColor(Color.TRANSPARENT);
            sdvPhoto.getHierarchy()
                    .setRoundingParams(roundingParams);
            sdvPhoto.setImageURI(item.avatarUrl);
        } else {
            if (item.isSelect == 1) {

                LogUtil.e("===datachange==");
                try {
                    roundingParams.setBorderColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
                    GenericDraweeHierarchy hierarchy = sdvPhoto.getHierarchy();
                    hierarchy
                            .setRoundingParams(roundingParams);
                    sdvPhoto.setAlpha(1f);
                    Uri uri = Uri.parse(item.avatarUrl);
                    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(72, 98))
                            .build();
                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(sdvPhoto.getController())
                            .setImageRequest(request)
                            .build();
                    sdvPhoto.setController(controller);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    sdvPhoto.setAlpha(0.6f);
                    roundingParams.setBorderColor(Color.TRANSPARENT);
                    sdvPhoto.getHierarchy()
                            .setRoundingParams(roundingParams);
                    Uri uri = Uri.parse(item.avatarUrl);
                    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(72, 98))
                            .build();
                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(sdvPhoto.getController())
                            .setImageRequest(request)
                            .build();
                    sdvPhoto.setController(controller);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView sdvPhoto;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    PictureSelectListener selectListener;

    public void setOnPictureSelectListener(PictureSelectListener onPictureSelectListener) {
        selectListener = onPictureSelectListener;
    }

    public interface PictureSelectListener {
        void onSelect(int position);
    }
}
